﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Linq;

namespace PaulBeck.Timesheet.WebParts.Admin.EditProject
{
    public partial class EditProjectUserControl : UserControl
    {

        public int ClientId
        {
            get
            {
                object o = ViewState["ClientId"];
                return (o == null) ? 0 : (int)o;
            }
            set
            {
                ViewState["ClientId"] = value;
            }
        }

        public int ProjectId
        {
            get
            {
                object o = ViewState["ProjectId"];
                return (o == null) ? 0 : (int)o;
            }
            set
            {
                ViewState["ProjectId"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ClientId = Convert.ToInt32(Page.Request.QueryString["cid"]);
                ProjectId = Convert.ToInt32(Page.Request.QueryString["pid"]);
                if (ProjectId == 0)  // Insert/Add new
                {
                    cbActive.Checked = true;
                    btnAdd.Visible = true;
                    btnEdit.Visible = false;
                    btnDelete.Visible = false;
                    // Get the approprate lable for the client
                    TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
                    try
                    {
                        var client = (from c in dc.Clients
                                      where c.ClientId == ClientId
                                      select new
                                  {
                                      c.Name,
                                  }).Single();
                        if (client != null)
                        {
                            lblClient.Text = client.Name;
                        }
                    }
                    catch(InvalidOperationException ex)
                    {

                    }
                }
                else
                {
                    btnAdd.Visible = false;
                    btnEdit.Visible = true;
                    btnDelete.Visible = true;
                    // Edit and delete is available
                    TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
                    var project = (from p in dc.Projects
                                   where p.ProjectId == ProjectId
                                  select new
                                  {
                                      p.Description,
                                      p.Active,
                                      Client = p.Client.Name,
                                  }).Single();
                    if (project != null)
                    {
                        lblClient.Text = project.Client;
                        txtDesc.Text = project.Description;
                        if (project.Active == true)
                        {
                            cbActive.Checked = true;
                        }
                    }
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
            Project project = new Project();
            project.ClientId = ClientId;
            project.Description = txtDesc.Text;
            project.Active = cbActive.Checked;
            project.CreatedDate = DateTime.Now;
            project.CreatedBy = CommonWFE.CurrentUserName();
            dc.Projects.InsertOnSubmit(project);
            dc.SubmitChanges();
            Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup('ProjectAdd');</script>");
            Context.Response.Flush();
            Context.Response.End();
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
            Project project = dc.Projects.Single(p => p.ProjectId == ProjectId);
            project.Description = txtDesc.Text;
            project.Active = cbActive.Checked;
            project.LastUpdatedDate = DateTime.Now;
            project.LastUpdatedBy = CommonWFE.CurrentUserName();
            dc.SubmitChanges();
            Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup('ProjectEdit');</script>");
            Context.Response.Flush();
            Context.Response.End();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
                Project project = dc.Projects.Single(p => p.ProjectId == ProjectId);
                dc.Projects.DeleteOnSubmit(project);
                dc.SubmitChanges();
                Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup('ProjectDelete');</script>");
                Context.Response.Flush();
                Context.Response.End();
            }
            catch (Exception ex)
            {
                lblClient.Text = "The Project cannot be deleted as it has dependancies.  <br/>Please ensure that resources to the project are removed before deleting the project.";
            }



        }
    }
}
